package cn.yh.nft.turbo.datasource.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * @ClassName: DataObjectHandler
 * @Author: 草莓爱上啵啵
 */
public class DataObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByNameIfNull("gmtCreate", new Date(), metaObject);
        this.setFieldValByNameIfNull("gmtModified", new Date(), metaObject);
        this.setFieldValByName("deleted", 0, metaObject);
        this.setFieldValByName("lockVersion", 0, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("gmtModified", new Date(), metaObject);
    }

    public void setFieldValByNameIfNull(String fieldName, Object fieldVal, MetaObject metaObject){
        if (metaObject.getValue(fieldName) == null){
            this.setFieldValByName(fieldName, fieldVal, metaObject);
        }
    }
}
